package com.etc.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.dao.*;
import com.etc.entity.*;
import com.etc.service.impl.AnsAnswerServiceDaoImpl;
import com.etc.service.impl.AnsMessageServiceDaoImpl;
import com.etc.service.impl.MessageServiceImpl;
import com.etc.service1.AnsAnswerServiceDao;
import com.etc.service1.AnsMessageServiceDao;
import com.etc.service1.MessageServiceDao;
import com.etc.util.PageTool;



/**
 * Servlet implementation class AnsMessageServlet
 */
@WebServlet("/AnsMessageServlet")
public class AnsMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AnsMessageServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		String am = request.getParameter("am");
		// System.out.println(am);
		if ("detail" == am || "detail".equals(am)) {
			detailmessage(request, response);
		} else if ("answer" == am || "answer".equals(am)) {
			answermessage(request, response);
		} else if ("myans" == am || "myans".equals(am)) {
			myansmessage(request, response);
		} else if ("feng" == am || "feng".equals(am)) {
			fengansmessage(request, response);
		} else if ("jie" == am || "jie".equals(am)) {
			jieansmessage(request, response);
		} else if ("del" == am || "del".equals(am)) {
			delansmessage(request, response);
		} else if ("all" == am || "all".equals(am)) {
			allansmessage(request, response);
		}else if("fengans"==am|| "fengans".equals(am)) {
			fengans(request, response);
		}else if("jieans"==am||"jieans".equals(am))
		{
			jieans(request, response);
		}
		String type = request.getParameter("type");
	}

	// ÿ�����ӵ�����
	protected void detailmessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		int id = Integer.parseInt(request.getParameter("id"));
		// System.out.println("id="+id);
		MessageServiceDao dao = new MessageServiceImpl();
		Message message = dao.findMessage(id);

		AnsMessageServiceDao amdao = new AnsMessageServiceDaoImpl();
		List<AnsMessage> anslist = new ArrayList<AnsMessage>();
		anslist = amdao.findTitleAnsMessage(id);
//		IAnsAnswerDao aadao=new AnsAnswerDaoImpl();
//		List<AnsAnswer> ansanslist = new ArrayList<AnsAnswer>();
//		//�����۵�����
//		ansanslist=aadao.findAnswerAnsMessage(id);
//		request.getSession().setAttribute("ansanslist", ansanslist);
		request.getSession().setAttribute("anslist", anslist);
		request.getSession().setAttribute("message", message);
		request.getRequestDispatcher("MessageDetail.jsp").forward(request, response);
	}

	// ����
	protected void answermessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		String username = request.getParameter("username");
		String title = request.getParameter("title");
		String type = request.getParameter("type");
		String content = request.getParameter("content");
		int ansid = Integer.parseInt(request.getParameter("ansid"));
		// System.out.println(username+title+type+content+ansid);
		AnsMessage ansmessage = new AnsMessage();
		ansmessage.setTitle(title);
		ansmessage.setUsername(username);
		ansmessage.setType(type);
		ansmessage.setContent(content);
		ansmessage.setAnsid(ansid);
		AnsMessageServiceDao amdao = new AnsMessageServiceDaoImpl();
		if (amdao.answerMessage(ansmessage)) {
			// System.out.println("ok");
			// detailmessage(request,response);
			// ����Ҳ����
			MessageServiceDao dao = new MessageServiceImpl();
			Message message = dao.findMessage(ansid);
			List<AnsMessage> anslist = new ArrayList<AnsMessage>();
			anslist = amdao.findTitleAnsMessage(ansid);
			request.getSession().setAttribute("anslist", anslist);
			request.getSession().setAttribute("message", message);
			request.getRequestDispatcher("MessageDetail.jsp").forward(request, response);
		} else {
			System.out.println("ans-no");
		}
	}

	// �ҵ�����
	protected void myansmessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		String username = request.getParameter("username");
		/*
		 * AnsMessageServiceDao amdao = new AnsMessageServiceDaoImpl(); List<AnsMessage>
		 * amlist = amdao.findUserAnsMessage(username);
		 * 
		 * AnsAnswerServiceDao aadao = new AnsAnswerServiceDaoImpl(); List<AnsAnswer>
		 * aalist = aadao.findUserAnsAnswer(username);
		 */
		int pageindex=Integer.valueOf(request.getParameter("pageIndex"));
		int pagesize = 10;
		AnsMessageServiceDao ms = new AnsMessageServiceDaoImpl();
		/* List<Message> list = ms.searchAllMessage(pageindex,pagesize,type); */ 
		List<AnsMessage> list = ms.myAns(username, pageindex, pagesize);
		PageTool<AnsMessage> pageMessage = new PageTool<AnsMessage>();
		pageMessage.setList(list);
		pageMessage.setPageIndex(pageindex);		
		pageMessage.setPageSize(pagesize);
		Long result=ms.countAll();
		pageMessage.setTotalCount(result.intValue());
		
		request.setAttribute("amlist", pageMessage);
		request.getRequestDispatcher("html/myans-list.jsp").forward(request, response);

	}

	protected void allansmessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		String username = request.getParameter("username");
		int pageindex=Integer.valueOf(request.getParameter("pageIndex"));
		int pagesize = 10;
		AnsMessageServiceDao ms = new AnsMessageServiceDaoImpl();
		/* List<Message> list = ms.searchAllMessage(pageindex,pagesize,type); */ 
		List<AnsMessage> list = ms.myAns(username, pageindex, pagesize);
		PageTool<AnsMessage> pageMessage = new PageTool<AnsMessage>();
		pageMessage.setList(list);
		pageMessage.setPageIndex(pageindex);		
		pageMessage.setPageSize(pagesize);
		Long result=ms.countAll();
		pageMessage.setTotalCount(result.intValue());
	   
		request.setAttribute("type", "all");
		request.setAttribute("amlist", pageMessage);
		request.getRequestDispatcher("html/allans-list.jsp").forward(request, response);

	}

	// �������
	protected void fengansmessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		int id = Integer.parseInt(request.getParameter("id"));
		String username1 =(String) request.getSession().getAttribute("username");
		String type=request.getParameter("type");
		AnsMessageServiceDao amdao = new AnsMessageServiceDaoImpl();
		if (amdao.closeAnsMessage(id)) {
			if(type.equals("my")) {
			request.getRequestDispatcher("AnsMessageServlet?am=myans&pageIndex=1&username="+username1).forward(request, response);
			}
			else {
				request.getRequestDispatcher("AnsMessageServlet?am=all&pageIndex=1").forward(request, response);
			}
		} else {
			System.out.println("feng-no");
		}
	}
	protected void fengans(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		int id = Integer.parseInt(request.getParameter("id"));
		int ansid = Integer.parseInt(request.getParameter("ansid"));
		AnsMessageServiceDao amdao=new AnsMessageServiceDaoImpl();
		if(amdao.closeAnsMessage(id))
		{
			//System.out.println("feng-yes");
			
			MessageServiceDao dao = new MessageServiceImpl();
			Message message=dao.findMessage(ansid);
			List<AnsMessage> anslist = new ArrayList<AnsMessage>();
			anslist=amdao.findTitleAnsMessage(ansid);
			request.getSession().setAttribute("anslist", anslist);
			request.getSession().setAttribute("message", message);
			request.getRequestDispatcher("MessageDetail.jsp").forward(request, response);
		}
		else
		{
			System.out.println("feng-no");
		}
	}
	// �������
	protected void jieansmessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		int id = Integer.parseInt(request.getParameter("id"));
		String username1 =(String) request.getSession().getAttribute("username");
		String type=request.getParameter("type");
		AnsMessageServiceDao amdao = new AnsMessageServiceDaoImpl();
		if (amdao.openAnsMessage(id)) {
			if(type.equals("my")) {
			request.getRequestDispatcher("AnsMessageServlet?am=myans&pageIndex=1&username="+username1).forward(request, response);
			}
			else {
				request.getRequestDispatcher("AnsMessageServlet?am=all&pageIndex=1").forward(request, response);
			}
		} else {
			System.out.println("feng-no");
		}
	}
	protected void jieans(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		int id = Integer.parseInt(request.getParameter("id"));
		int ansid = Integer.parseInt(request.getParameter("ansid"));
		AnsMessageServiceDao amdao=new AnsMessageServiceDaoImpl();
		if(amdao.openAnsMessage(id))
		{
			//System.out.println("jie-yes");
			
			MessageServiceDao dao = new MessageServiceImpl();
			Message message=dao.findMessage(ansid);
			List<AnsMessage> anslist = new ArrayList<AnsMessage>();
			anslist=amdao.findTitleAnsMessage(ansid);
			request.getSession().setAttribute("anslist", anslist);
			request.getSession().setAttribute("message", message);
			request.getRequestDispatcher("MessageDetail.jsp").forward(request, response);
		}
		else
		{
			System.out.println("jie-no");
		}
	}
	// ɾ����
	protected void delansmessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		int id = Integer.parseInt(request.getParameter("id"));
		int ansid = Integer.parseInt(request.getParameter("ansid"));
		String title = request.getParameter("title");
		AnsMessageServiceDao amdao = new AnsMessageServiceDaoImpl();
		if (amdao.deleteAnsMessage(id) && amdao.deleteAnsAnsMessage(id)) {
			// System.out.println("jie-yes");

			MessageServiceDao dao = new MessageServiceImpl();
			Message message = dao.findMessage(ansid);
			List<AnsMessage> anslist = new ArrayList<AnsMessage>();
			anslist = amdao.findTitleAnsMessage(ansid);
			request.getSession().setAttribute("anslist", anslist);
			request.getSession().setAttribute("message", message);
			request.getRequestDispatcher("MessageDetail.jsp").forward(request, response);
		} else {
			System.out.println("jie-no");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
